package com.bxd.baodan.fragment;

import android.view.View;

import com.bumptech.glide.Glide;
import com.google.gson.reflect.TypeToken;
import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.activity.WebViewPager;
import com.bxd.baodan.adapter.ViewHolder;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.base.BaseListFragment;
import com.bxd.baodan.item.ProductItem;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.util.StringUtil;
import com.bxd.baodan.widget.ShapedImageView;
import com.umeng.analytics.MobclickAgent;

import java.lang.reflect.Type;
import java.util.List;

/**
 * 产品首页和收藏列表
 */

public class ProductCollectFragment extends BaseListFragment<ProductItem> {

    @Override
    protected void preDone(View view) {


        footer = View.inflate(getActivity(),R.layout.collect_null_footer,null);
        autoRefresh();
    }

    @Override
    protected Type getTypeToken() {
        return new TypeToken<List<ProductItem>>() {
        }.getType();
    }

    @Override
    protected int getFragmentLayout() {
        return R.layout.pull_refresh_layout_no_divider;
    }

    @Override
    protected int getItemLayout() {
        return R.layout.product_item;
    }

    @Override
    protected void convert(ViewHolder holder, final ProductItem item) {


        holder.setText(R.id.p_name, item.p_name);
        holder.setText(R.id.p_features, item.p_features);

        if (StringUtil.isEmpty(item.cat_name)) {
            holder.getView(R.id.cat_name).setVisibility(View.GONE);
        } else {
            holder.setText(R.id.cat_name, item.cat_name);
            holder.getView(R.id.cat_name).setVisibility(View.VISIBLE);
        }



        holder.setText(R.id.p_name, item.p_name);
        holder.setText(R.id.p_features, item.p_features);
        Glide.with(getActivity()).load(item.p_img).
                error(R.drawable.img_error).
                into(((ShapedImageView) holder.getView(R.id.p_image)));
        Glide.with(getActivity()).load(item.p_img).
                error(R.drawable.img_error).
                into(((ShapedImageView) holder.getView(R.id.p_image)));


        holder.getView(R.id.p_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(App.getContext(), "v6_1_3_WD_wdsc_cp_cpxq");
                openActivity(WebViewPager.class, new String[]{"startUrl"}, new String[]{item.p_url});
            }
        });

    }

    @Override
    protected String getArrayStr(String json_data) {
        return "list";
    }

    @Override
    protected void setUsualNetUtil() {
            netUtil = new NetUtil(getActivity(), JsonApi.PRODUCT_MYCOLLECT);
    }

}
